<template>
  <div class="app-container">
    <div class="filter-container filter-area">
      <!-- 搜索区域-->
      <label class="filter-item" style="margin-right: 10px;">协议模板名称:</label>
      <el-input
        v-model="listQuery.agreeName"
        placeholder="协议类型名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />

      <label class="filter-item" style="margin-right: 10px;">协议类型名称:</label>
      <el-input
        v-model="listQuery.elementTypeName"
        placeholder="协议类型名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />

    </div>

    <!-- 新增的操作按钮区 -->
    <div class="filter-container operation-area">
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 8px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        添加协议模板
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      :height="tableHeight"
      :header-cell-style="{ background: '#fafafa' }"
    >
      <el-table-column label="序号" prop="id" align="center" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="协议编码" min-width="80px">
        <template slot-scope="{row}">
          <span>{{ row.agreeCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="协议名称" min-width="120px">
        <template slot-scope="{row}">
          <span>{{ row.agreeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="协议类型编码" min-width="80px">
        <template slot-scope="{row}">
          <span>{{ row.agreeTypeCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="协议类型名称" min-width="100px">
        <template slot-scope="{row}">
          <span>{{ row.agreeTypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="协议状态" min-width="60px">
        <template slot-scope="{row}">
          <span>{{ row.stsName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="协议版本" min-width="60px">
        <template slot-scope="{row}">
          <span>{{ row.version }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" min-width="120px">
        <template slot-scope="{row}">
          <span>{{ row.createdTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" min-width="120px">
        <template slot-scope="{row}">
          <span>{{ row.updatedTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建工号" min-width="80px">
        <template slot-scope="{row}">
          <span>{{ row.createdNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建姓名" min-width="80px">
        <template slot-scope="{row}">
          <span>{{ row.createdName }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        width="240px"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            修改
          </el-button>
          <el-button type="success" size="mini" @click="handleConfig(row)">
            编辑模板
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row, $index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      class="pagination-container"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { agreeList } from '@/views/agree/data'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default {
  name: 'AgreeType',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableHeight: 'calc(100vh - 320px)',
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        userName__like: '',
        userNo: '',
        sts: ''
      }

    }
  },
  created() {
    this.getList()
    this.calculateTableHeight()
    window.addEventListener('resize', this.calculateTableHeight)
  },
  mounted() {
    // Watch for DOM changes that might affect filter area height
    this.observer = new MutationObserver(this.calculateTableHeight)
    this.observer.observe(document.querySelector('.filter-area'), {
      childList: true,
      subtree: true,
      attributes: true
    })
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calculateTableHeight)
  },
  methods: {
    getList() {
      this.listLoading = false
      this.list = agreeList
      this.total = agreeList.length
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        userName: '',
        userEmail: '',
        userPwd: '',
        roles: [],
        sts: '0'
      }
    },
    handleCreate() {
      this.$notify({
        title: '成功',
        message: '添加数据逻辑',
        type: 'success',
        duration: 2000
      })
    },
    handleDelete() {
      this.$notify({
        title: '成功',
        message: '删除数据逻辑',
        type: 'success',
        duration: 2000
      })
    },
    handleUpdate(row) {
      this.$notify({
        title: '成功',
        message: '修改数据逻辑',
        type: 'success',
        duration: 2000
      })
    },
    handleConfig(row) {
      const id = row.id
      this.$router.push(`/agree/config/${id}`)
    },
    // 表格高度计算
    calculateTableHeight() {
      const filterHeight = document.querySelector('.filter-area')?.offsetHeight || 0
      const operationHeight = document.querySelector('.operation-area')?.offsetHeight || 0
      const paginationHeight = 60
      const padding = 40

      this.tableHeight = `calc(100vh - ${filterHeight + operationHeight + paginationHeight + padding}px)`
      this.$nextTick(() => {
        this.tableKey++ // Force table re-render
      })
    }
  }
}
</script>

<style scoped></style>
